package com.shenmazong.serverwebpoem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shenmazong.serverwebpoem.mapper.ITbRecordLoginMapper;
import com.shenmazong.serverwebpoem.mapper.ITbRecordStone;
import com.shenmazong.serverwebpoem.mapper.ITbUserMapper;
import com.shenmazong.serverwebpoem.pojo.TbRecordLogin;
import com.shenmazong.serverwebpoem.pojo.TbRecordStone;
import com.shenmazong.serverwebpoem.pojo.TbUser;
import com.shenmazong.serverwebpoem.service.TbUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 田不平
 * @project server-web-poem
 * @date 2020/3/24 10:10 AM
 * http://www.shenmazong.com
 */

@Slf4j
@Service
public class TbUserServiceImpl implements TbUserService {

    @Autowired
    ITbUserMapper iTbUserMapper;

    @Autowired
    ITbRecordLoginMapper iTbRecordLoginMapper;

    @Autowired
    ITbRecordStone iTbRecordStone;

    @Override
    public void add(TbUser user) {
        iTbUserMapper.insert(user);
    }

    @Override
    public void del(Integer id) {
        iTbUserMapper.deleteById(id);
    }

    @Override
    public void set(TbUser user) {
        iTbUserMapper.updateById(user);
    }

    @Override
    public TbUser getUserById(Integer id) {
        return iTbUserMapper.selectById(id);
    }

    @Override
    public TbUser getUserByName(String name) {
        return iTbUserMapper.selectOne(new QueryWrapper<TbUser>().eq("username", name));
    }

    /**
     * TODO 增加登录记录
     * @param recordLogin
     */
    @Override
    public void addRecordLogin(TbRecordLogin recordLogin) {
        iTbRecordLoginMapper.insert(recordLogin);
    }

    /**
     * TODO 根据用户ID和日期，获取登录记录
     * @param userId
     * @param loginDate
     * @return
     */
    @Override
    public TbRecordLogin getUserRecordByDate(Integer userId, Integer loginDate) {
        QueryWrapper queryWrapper = new QueryWrapper<TbRecordLogin>();
        queryWrapper.eq("userid", userId);
        queryWrapper.eq("logindate", loginDate);
        queryWrapper.last("limit 1");
        return iTbRecordLoginMapper.selectOne(queryWrapper);
    }

    /**
     * TODO 增加用户的积分记录以及积分余额
     * @param user
     */
    @Override
    public void addRecordStoneById(TbUser user, Long scores, String reason) {

        //-- 增加积分记录
        TbRecordStone stone = new TbRecordStone();
        stone.setUserid(user.getId());
        stone.setLaststone(user.getUserstone());
        stone.setChangestone(scores);
        stone.setUserstone(user.getUserstone()+scores);
        stone.setReason(reason);

        iTbRecordStone.insert(stone);

        //-- 更新账户余额
        user.setUserstone(user.getUserstone()+scores);
        iTbUserMapper.updateById(user);

        return;
    }

    /**
     * TODO 获取用户积分变更记录
     * @param userid
     * @return
     */
    @Override
    public IPage<TbRecordStone> getPageStoneByUserId(Integer userid, Integer pageCurrent, Integer pageSize) {
        QueryWrapper<TbRecordStone> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", userid);
        queryWrapper.orderByDesc("id");

        Page<TbRecordStone> page = new Page<>(pageCurrent, pageSize);
        IPage<TbRecordStone> iPage = iTbRecordStone.selectPage(page, queryWrapper);
        System.out.println("总页数:"+iPage.getPages());
        System.out.println("总记录数:"+iPage.getTotal());

        return iPage;
    }
}
